package com.lenin.omena.server;

import javax.jdo.JDOHelper;
import javax.jdo.Transaction;

import com.lenin.omena.shared.domain.Produce;
import com.lenin.omena.shared.domain.User;
import com.lenin.omena.shared.domain.UserInfo;


public class UserInfoDAO extends JdoDAO<Long, UserInfo> {

	public UserInfoDAO() {
		super();
	}
	
	
	public UserInfo saveUserInfo(UserInfo userInfo, UserInfo detachedUserInfo) {
		
		
		Object jdoId1 = JDOHelper.getObjectId(userInfo);
		Object jdoId2 = JDOHelper.getObjectId(detachedUserInfo);
		
		boolean eq = jdoId1 != null && jdoId1.equals (jdoId2);
		
		
		Transaction tx = pm.currentTransaction();

		UserInfo entity = null;
		
		try
		{
		    tx.begin(); // Start the PM transaction

		    userInfo.setUserName(detachedUserInfo.getUserName());
		    userInfo.setPassword(detachedUserInfo.getPassword());
		    userInfo.setFirstName(detachedUserInfo.getFirstName());
		    userInfo.setEmail(detachedUserInfo.getEmail());
		    userInfo.setPhoneNumber(detachedUserInfo.getPhoneNumber());
		    userInfo.setSiteAddressKey(detachedUserInfo.getSiteAddress().getKey());
		    
		    entity = pm.makePersistent(userInfo);

		    tx.commit(); // Commit the PM transaction
		}
		finally
		{
		    if (tx.isActive())
		    {
		        tx.rollback(); // Error occurred so rollback the PM transaction
		    }
		}
		
		return entity;
		
		
	}
	
	
}
